package com.leyou.page.service.impl;

import com.leyou.item.pojo.*;
import com.leyou.page.client.BrandClient;
import com.leyou.page.client.CategoryClient;
import com.leyou.page.client.GoodsClient;
import com.leyou.page.client.SpecificationClient;
import com.leyou.page.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品详情页的数据Service
 * @Date 2018/12/28  20:48
 * @Author JIE
 */
@Service
public class PageServiceImpl implements PageService {


    @Autowired
    private BrandClient brandClient;

    @Autowired
    private CategoryClient categoryClient;

    @Autowired
    private GoodsClient goodsClient;

    @Autowired
    private SpecificationClient specClient;

    @Override
    public Map<String, Object> loadModel(Long spuId) {
        Map<String, Object> model=new HashMap<>();
        //查询spu
        Spu spu = goodsClient.querySpuById(spuId);
        //从spu中获取skus
        List<Sku> skus = spu.getSkus();
        //从spu中获得detail
        SpuDetail detail = spu.getSpuDetail();
        //查询brand的
        Brand brand = brandClient.queryBrandByid(spu.getBrandId());
        //查询商品的分类categorie
        List<Category> categories = categoryClient.queryCategoryByIds(
                Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3()));
        //查询规格参数
        List<SpecGroup> specs = specClient.queryGroupByCid(spu.getCid3());
        String title = spu.getTitle();
        model.put("title", title);
        model.put("subTitle",spu.getSubTitle());
//        model.put("spu",spu);
        model.put("skus",skus);
        model.put("detail",detail);
        model.put("brand",brand);
        model.put("categories",categories);
        model.put("specs",specs);

        return model;
    }
}
